{%- macro form_errors(form) %}
  {%- if form.errors %}
    {%- for fieldname, errors in form.errors.items() %}
      {%- if bootstrap_is_hidden_field(form[fieldname]) %}
        {%- for error in errors %}
          <p class="form-text text-danger">{{error}}</p>
        {%- endfor %}
      {%- endif %}
    {%- endfor %}
  {%- endif %}
{%- endmacro %}

{%- macro form_field_errors(field) %}
  {%- if field.errors %}
    {%- for error in field.errors %}
      <p class="form-text text-danger">{{ error }}</p>
    {%- endfor %}
  {%- endif %}
{%- endmacro %}

{%- macro form_fields(fields, prepend='', append='', label=True) %}
  {%- set width = (12 / fields|length)|int %}
  <div class="form-group">
    <div class="row">
      {%- for field in fields %}
      <div class="col-lg-{{ width }} col-xs-12">
        {%- if field.__class__.__name__ == 'list' %}
          {%- for subfield in field %}
            {{ form_individual_field(subfield, prepend=prepend, append=append, label=label, **kwargs) }}
          {%- endfor %}
        {%- else %}
          {{ form_individual_field(field, prepend=prepend, append=append, label=label, **kwargs) }}
        {%- endif %}
      </div>
      {%- endfor %}
    </div>
  </div>
{%- endmacro %}

{%- macro form_individual_field(field, prepend='', append='', label=True, class_="") %}
  {%- set fieldclass=" ".join(["form-control"] + ([class_] if class_ else []) + (["is-invalid"] if field.errors else [])) %}
  {%- if field.type == "BooleanField" %}
    {{ field(**kwargs) }}<span>&nbsp;&nbsp;</span>{{ field.label if label else '' }}
  {%- else %}
    {{ field.label if label else '' }}{{ form_field_errors(field) }}
    {%- if prepend %}<div class="input-group-prepend">{%- elif append %}<div class="input-group-append">{%- endif %}
      {{ prepend|safe }}{{ field(class_=fieldclass, **kwargs) }}{{ append|safe }}
    {%- if prepend or append %}</div>{%- endif %}
  {%- endif %}
{%- endmacro %}

{%- macro form_field(field) %}
  {%- if field.type == 'SubmitField' %}
  {{- form_fields((field,), label=False, class="btn btn-default", **kwargs) }}
  {%- else %}
  {{- form_fields((field,), **kwargs) }}
  {%- endif %}
{%- endmacro %}

{%- macro form(form) %}
<form class="form" method="post" role="form">
  {{ form.hidden_tag() }}
  {%- for field in form %}
    {%- if not bootstrap_is_hidden_field(field) %}
      {{ form_field(field) }}
    {%- endif %}
  {%- endfor %}
</form>
{%- endmacro %}

{%- macro card(title=None, theme="primary", header=True) %}
<div class="row">
  <div class="col-lg-12">
    <div class="card card-outline card-{{ theme }}">
      {%- if header %}
      <div class="card-header border-0">
        {%- if title %}
        <h3 class="card-title">{{ title }}</h3>
        {%- endif %}
      </div>
      {%- endif %}
      <div class="card-body">
       {{- caller() }}
      </div>
    </div>
  </div>
</div>
{%- endmacro %}

{%- macro table(title=None, theme="primary", datatable=True, order=None) %}
<div class="row">
  <div class="col-lg-12">
    <div class="card card-outline card-{{ theme }}">
      {%- if title %}
      <div class="card-header border-0">
        <h3 class="card-title">{{ title }}</h3>
      </div>
      {%- endif %}
      <div class="card-body">
        <table class="table table-bordered{% if datatable %} dataTable{% endif %}" data-order="{{ order or '[]' | e }}">
          {{- caller() }}
        </table>
      </div>
    </div>
  </div>
</div>
{%- endmacro %}

{%- macro fieldset(title=None, field=None, enabled=None, fields=None) %}
{%- if field or title %}
<fieldset{% if not enabled %} disabled{% endif %}>
{%- if field %}
<legend>{{ form_individual_field(field) }}</legend>
{%- else %}
<legend>{{ title }}</legend>
{%- endif %}
{%- endif %}
{{- caller() }}
{%- if fields %}
  {%- set kwargs = {"enabled" if enabled else "disabled": ""} %}
  {%- for field in fields %}
    {{ form_field(field, **kwargs) }}
  {%- endfor %}
{%- endif %}
</fieldset>
{%- endmacro %}

{%- macro clip(target, title=_("copy to clipboard"), icon="copy", color="primary", action="copy") %}
<button class="btn btn-{{ color }} btn-xs btn-clip float-right ml-2 mt-1" data-clipboard-action="{{ action }}" data-clipboard-target="#{{ target }}">
<i class="fas fa-{{ icon }}" title="{{ title }}" aria-expanded="false"></i><span class="sr-only">{{ title }}</span>
</button>
{%- endmacro %}
